package handler

import (
	"context"
	"evaluation/api/request"
	"evaluation/global"
	"evaluation/internal/model"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

func AddBanned(c *gin.Context) {
	var req request.AddBannedForm
	var banned model.Banned

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	banned.Banned = req.Banned

	err := banned.AddBanned()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "添加失败",
			"data":    "",
		})
		return
	}

	global.RDB.HSet(context.Background(), fmt.Sprintf("banned:%d", banned.Id), banned.Banned)

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "添加成功",
		"data":    banned.Id,
	})
}

func UpdateBanned(c *gin.Context) {
	var req request.UpdateBannedForm
	var banned model.Banned

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	banned.Id = req.Id
	banned.Banned = req.Banned

	err := banned.UpdatedBanned()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "修改失败",
			"data":    "",
		})
		return
	}

	global.RDB.HSet(context.Background(), fmt.Sprintf("banned:%d", banned.Id), banned.Banned)

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "修改成功",
		"data":    banned.Id,
	})

}

func DeletedBanned(c *gin.Context) {
	var req request.DeletedBannedForm
	var banned model.Banned

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	banned.Id = req.Id

	err := banned.DeletedBanned()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "删除失败",
			"data":    "",
		})
		return
	}

	global.RDB.Del(context.Background(), fmt.Sprintf("banned:%d", banned.Id))

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "删除成功",
		"data":    "",
	})
}

func GetBannedNum(c *gin.Context) {

}
